<template>
  <div>
    <HomeHeader></HomeHeader>
    <HomeSwiper :list="SwiperList"></HomeSwiper>
    <HomeIcons :list="IconList"></HomeIcons>
    <HomeRecommend :list="RecommendList"></HomeRecommend>
    <HomeWeekend :list="WeekendList"></HomeWeekend>
  </div>
</template>

<script>
//导入头部组件
import HomeHeader from './components/Header.vue'
//导入轮播图组件
import HomeSwiper from './components/Swiper.vue'
//导入首页图标组件
import HomeIcons from './components/Icons.vue'
//导入推荐组件
import HomeRecommend from './components/Recommend.vue'
//导入周末去哪儿组件
import HomeWeekend from './components/Weekend.vue'
//导入axios
import axios from 'axios'
//导入计算属性以返回 Vuex store 中的状态的API
import {mapState} from 'vuex'

export default {
  data(){
    return{
      lastCity:'',//保存上一次点击的城市
      SwiperList:[],
      IconList:[],
      RecommendList:[],
      WeekendList:[]
    }
  },
  components:{
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  computed:{
    ...mapState(['city'])
  },
  created(){
    this.lastCity=this.city
    this.getHomeInfo()
  },
  activated(){
    if (this.lastCity !== this.city) {
      this.lastCity=this.city
      this.getHomeInfo()
    }
  },
  methods:{
    getHomeInfo(){//获取home页数据
      axios.get('/api/index.json?city='+this.city)
          .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc(res){
      if (res.data.ret&&res.data.data) {
        this.SwiperList=res.data.data.swiperList
        this.IconList=res.data.data.iconList
        this.RecommendList=res.data.data.recommendList
        this.WeekendList=res.data.data.weekendList
      }
    }
  }
}
</script>

<style scoped>

</style>

